package com.summerli.controller;

import com.alibaba.fastjson.JSONObject;
import com.summerli.common.ResponseResult;
import com.summerli.common.seriviceverificationcode.VerifyCodeRequest;
import com.summerli.service.VerifyCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verify-code")
@Slf4j
public class VerifyCodeController {
    @Autowired
    private VerifyCodeService verifyCodeService;

    /**
     * 生成校验码
     * @param identity
     * @param phoneNumber
     * @return
     */
    @GetMapping("/generate/{identity}/{phoneNumber}")
    public ResponseResult generate(@PathVariable("identity") int identity , @PathVariable ("phoneNumber") String phoneNumber){
        log.info("身份类型："+identity+",手机号："+phoneNumber);
        return verifyCodeService.generate(identity,phoneNumber);
    }

    /**
     * 校验校验码
     * @param request
     * @return
     */
    @PostMapping("/verify")
    public ResponseResult verify(@RequestBody VerifyCodeRequest request){
        log.info("/verify-code/verify request:"+ JSONObject.toJSONString(request));
        int identity = request.getIdentity();
        String phoneNumber = request.getPhoneNumber();
        String code = request.getCode();
        return verifyCodeService.verify(identity,phoneNumber,code);
    }
}
